package edu.luc.cs.trull.demo.wristwatch;

import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import edu.luc.cs.trull.Scheduling;

/**
 * A driver for running this demo.
 */
public class Main implements EventLabels {
	
  public static void main(String[] args) throws Exception {
		BasicConfigurator.configure();
	  Logger.getRootLogger().setLevel(Level.OFF);
//	  Logger.getLogger(AbstractEmit.class).setLevel(Level.DEBUG);
	  Scheduling.setMode(Scheduling.SWINGFAST);
      
	  // the control logic
  	 final WristWatch wristwatch = new WristWatch();
    
    // Converts an ActionEvent to a LabeledEvent, using the action command
    // as the event label.
    ActionListener wristwatchListener = new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        String label = event.getActionCommand();
        System.out.println(label == null ? TICK : label);
        PropertyChangeEvent e = new PropertyChangeEvent(this, label == null ? TICK : label,null, label == null ? TICK : label);
        wristwatch.propertyChange(e);
      }
    };

    // the off-the-shelf components
    final JButton start = new JButton("Start/Stop");
    start.setActionCommand(START);
    final JButton reset = new JButton("Reset/Lap");
    reset.setActionCommand(RESET);
    final JButton light = new JButton(LIGHT);
    final JButton mode = new JButton(MODE);
    final JLabel display = new JLabel("8 8 : 8 8", JLabel.CENTER);
    display.setOpaque(true);
    display.setBackground(Color.lightGray);
    final JLabel modeIndicator = new JLabel(MODE, JLabel.CENTER);
    modeIndicator.setBackground(Color.lightGray);
    final Timer timer = new Timer(1000, wristwatchListener);

    // hook the components up to the control logic
    start.addActionListener(wristwatchListener);
    reset.addActionListener(wristwatchListener);
    light.addActionListener(wristwatchListener);
    mode.addActionListener(wristwatchListener);

    wristwatch.addPropertyChangeListener(new PropertyChangeListener() {
    public void propertyChange(PropertyChangeEvent event){
    	Object label = event.getPropertyName();
    	if (NOW.equals(label)) {
    		int val = ((Integer) event.getNewValue()).intValue();
    		display.setText(
    				(val / DateModel.SEC_PER_MIN / 10) + " " + (val / DateModel.SEC_PER_MIN % 10) + " : " +
					(val % DateModel.SEC_PER_MIN / 10) + " " + (val               % 10)
    		);
    	} else if (LIGHT_ON.equals(label)) {
    		display.setBackground(Color.yellow);
    		modeIndicator.setBackground(Color.yellow);
    	} else if (LIGHT_OFF.equals(label)) {
    		display.setBackground(Color.lightGray);
    		modeIndicator.setBackground(Color.lightGray);
    	} else if (TIME.equals(label)) {
    		modeIndicator.setText("Time");
    	} else if (DATE.equals(label)) {
    		modeIndicator.setText("Date");
    	} else if (STOP.equals(label)
    		| LAP.equals(label)
				| HOUR.equals(label)
				| MIN.equals(label)
				| DAY.equals(label)
				| MONTH.equals(label)) {
    		modeIndicator.setText((String) label);
    	}
    }
    });

    // create visual interface
    final JFrame frame = new JFrame("WristWatch");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane(); 

    contentPane.setLayout(new GridLayout(2, 3, 10, 10));
    contentPane.add(start);
    contentPane.add(reset);
    contentPane.add(mode);
    contentPane.add(light);
    contentPane.add(display);
    contentPane.add(modeIndicator);

    // start wristwatch
    Scheduling.getScheduler().start(wristwatch);
    
    // start timer
    timer.start();

    frame.pack();
    frame.setVisible(true);
  }
}
